﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EventThresholdReached
{
    public class Counter   //事件的发布者类
    {
        private int threshold;//事件订阅者
        private int total;//事件订阅者
        public Counter(int pasThreshold)//事件参数类
        {
            threshold = pasThreshold;
        }
        public void Add(int x)
        {
            total += x;
            if (total >= threshold)
            {
                ThresholdReachedEventArgs args = new ThresholdReachedEventArgs();
                args.Threshold = threshold;
                args.Time = DateTime.Now;
                OnThresholReadched(args);
            }
        }

        private void OnThresholReadched(ThresholdReachedEventArgs t)//事件订阅者类
        {
            ThresholdReached?.Invoke(this, t);
            //throw new NotImplementedException();
        }
        public event EventHandler<ThresholdReachedEventArgs> ThresholdReached;//事件处理器

    }
}
